<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/27
 * Time: 9:54
 */

namespace app\api\model;

class ShopCar extends Base
{
    /**
     * 多条联查
     * @param $where
     * @param $field
     * @param $page
     * @param $type   1.购物车列表   2.确认订单展示页  3.提交购物车订单
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static function selectJoinData($where = [], $field = '*', $page = 1, $type = 1){
        $query = self::join('up_goods', 'goods_id = sc_goods_id', 'LEFT')
            ->join('up_goods_spec', 'spec_id = sc_spec_id', 'LEFT')
            ->where($where)
            ->field($field)
            ->order('sc_time desc');
        if($type == 1){
            $query->join('up_goods_packaging', 'packaging_id = sc_packaging_id', 'LEFT')
                ->page($page, self::$limit);
        }elseif($type == 2){
            $query->join('up_goods_packaging', 'packaging_id = sc_packaging_id', 'LEFT');
        }elseif ($type == 3){

        }
        return $query->select();
    }

    /**
     * 新增记录
     * @param $insert
     * @return int|string
     */
    static function insertData($insert = []){
        return self::insert($insert);
    }

    /**
     * 单条查询
     * @param $where
     * @param $field
     * @return array|bool|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static function findData($where = [], $field = '*'){
        return self::where($where)->field($field)->find();
    }

    /**
     * 更新记录
     * @param $where
     * @param $update
     * @return ShopCar
     */
    static function updateData($where = [], $update = []){
        return self::where($where)->update($update);
    }

    /**
     * 删除记录
     * @param $where
     * @return false|int
     */
    static function delData($where = []){
        return self::where($where)->delete();
    }

    /**
     * 统计数量
     * @param $where
     * @return int|string
     * @throws \think\Exception
     */
    static function sumData($where = []){
        return self::where($where)->sum('sc_number');
    }
}